package com.hs.teachsystem.controller;

import com.hs.teachsystem.UserService;
import com.hs.teachsystem.model.Model;
import com.hs.teachsystem.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by MaiBenBen on 2017/7/14.
 */
@Controller
@RequestMapping("/user")
public class UserController extends MyController{
    @Autowired
    private UserService userService;
    /**
     * 登陆
     * @return
     */
    @RequestMapping("/login")
    public ModelAndView login(HttpServletRequest request,HttpServletResponse response, HttpSession session){
        User user=new User();
        user.setrID(Integer.valueOf(request.getParameter("uID")));
        user.setPassword(request.getParameter("password"));
        //登陆失败
        //记住用户，存入cookies
        String ifRemmberMe=request.getParameter("ifRemmberMe");
        //先在session上验证
        User sessionUser=(User)session.getAttribute("user");
        if(sessionUser!=null){
            if(sessionUser.getPassword().equals(user.getPassword()))
                return new ModelAndView((String) session.getAttribute("view"));
            else {
                Map<String,Object> result=new HashMap<String, Object>();
                result .put("message", "密码输入有误");
                return new ModelAndView("login",result);
            }
        }
        //进入数据库验证
        Map<String,Object> result=userService.login(user);
        //登陆成功
        if(!result.get("status").equals("200")){
            return new ModelAndView((String) result.get("view"),result);
        }
        //存入session
        session.setAttribute("user",((List<Model>)result.get("resultList")).get(0));
        session.setAttribute("view",result.get("view"));
        //页面跳转
        return new ModelAndView((String) result.get("view"));
    }

    /**
     * 添加用户
     * @return
     */
    @RequestMapping("/addUser")
    public String addUser(){
        return "";
    }

    /**
     * 批量添加用户
     * 通过导入excel表，自动解析导入到数据库中
     * @return
     */
    @RequestMapping("/addUserByList")
    public String addUserByList(){
        return "";
    }

    /**
     * 删除用户
     * @return
     */
    @RequestMapping("/deleteUser")
    public String deleteUser(){
        return "";
    }

    /**
     * 查询用户
     * @return
     */
    @RequestMapping("/queryUser")
    public Object queryUser(){
        Map<String,Object> message=null;
        return message;
    }

    /**
     * 完善个人信息
     * @return
     */
    @RequestMapping("/updateUserInfo")
    public ModelAndView updateUserInfo(){
        return new ModelAndView();
    }


}
